package com.wdg.dao.impl;

import com.wdg.dao.StuDao;
import com.wdg.pojo.Student_reg;
import com.wdg.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class StuDaoImpl implements StuDao {
    Connection cn = DBUtil.getCn();
    @Override
    public ArrayList<Student_reg> findStuByKey(String key) {
        ArrayList<Student_reg> slist=new ArrayList<>();
        String sql="SELECT * FROM student_reg WHERE username LIKE ?";
        try {
            PreparedStatement pst = cn.prepareStatement(sql);
            pst.setString(1,"%"+key+"%");
            ResultSet rs = pst.executeQuery();
            while (rs.next()){
                Student_reg u=new Student_reg();
                u.setId(rs.getInt("id"));
                u.setUsername(rs.getString("username"));
                u.setUserclass(rs.getString("userclass"));
                u.setSort(rs.getString("sort"));
                u.setTeacher(rs.getString("teacher"));
                slist.add(u);
            }
            rs.close();
            pst.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return slist;
    }
}
